package cn.bdqn.controller;

import cn.bdqn.pojo.Author;
import cn.bdqn.pojo.Book;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

@RestController
public class BookController {
    @GetMapping("/book")
    public Book book(){
        Book book = new Book();
        book.setName("罗贯中");
        book.setAuthor("三国演义");
        book.setPrice(30f);
        book.setPublicationDate(new Date());
        return book;
    }

    @GetMapping("/books")
    public String books(@ModelAttribute("b") Book book, @ModelAttribute("a")Author author){
        return book.toString()+">>>"+author.toString();
    }

    /*@GetMapping("/hello")
    public void hello(Model model){
        Map<String,Object> map = model.asMap();
        Set<String> ketSet = map.keySet();
        Iterator<String> iterator = ketSet.iterator();
        while (iterator.hasNext()){
            String key = iterator.next();
            Object value = map.get(key);
            System.out.println(key+">>>>>>"+value);
        }
    }*/

    @GetMapping("/hello")
    public String hello(){
        int i = 1/0;
        return "hello";
    }

    @PostMapping("/addBook")
    @CrossOrigin(value = "http://localhost:8081"
                ,maxAge = 1800,allowedHeaders = "*")
    public String addBook(String name){
        return "receive:"+name;
    }

    @DeleteMapping("book/{id}")
    @CrossOrigin(value = "http://localhost:8081"
            ,maxAge = 1800,allowedHeaders = "*")
    public String deleteBookById(@PathVariable Long id){
        return String.valueOf(id);
    }
}
